<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>药品出库</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<style type="text/css">
.item {
	margin: 4px
}
</style>
</head>
<body>
    <script type="text/javascript">
		$.extend($.fn.datagrid.methods, {
			editCell : function(jq, param) {
				return jq.each(function() {
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields', true)
							.concat($(this).datagrid('getColumnFields'));
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field) {
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});

		var editIndex = undefined;
		function endEditing() {
			if (editIndex == undefined) {
				return true
			}
			if ($('#lfq-tb-chuku3').datagrid('validateRow', editIndex)) {
				$('#lfq-tb-chuku3').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field) {
			if (endEditing()) {
				$('#lfq-tb-chuku3').datagrid('selectRow', index).datagrid(
						'editCell', {
							index : index,
							field : field
						});
				editIndex = index;
			}
		}
	</script>

	<script type="text/javascript">
		$(function() {
			//刷新
			$("#lfq-drug-reload2").linkbutton({
				onClick : function() {
					$("#lfq-tb-drugChuku").datagrid({
						url : 'selectHisInventoryAllDrug',
						method : 'post'
					});
					$("#lfq-drug-search2").searchbox("setValue", "");
				}
			});

			//搜索
			$('#lfq-drug-search2').searchbox({
				searcher : function(value, name) {
					if (value == null || value.length == 0) {

					} else {
						if (name == "药品名称") {
							$("#lfq-tb-drugChuku").datagrid({
								url : 'selectHisInventorylLikeBykcName',
								method : 'post',
								queryParams : {
									kcName : value
								}
							});
						} else if (name == "库存编号") {
							if (!isNaN(value)) {
								$("#lfq-tb-drugChuku").datagrid({
									url : 'selectHisInventorylLikeBykcNo',
									method : 'post',
									queryParams : {
										kcNo : value
									}
								});
							} else {
								$.messager.alert("提示", "药品编号是由数字组成，请输入正确的编号！");
							}
						}
					}
				}
			});

			//打开编辑窗口
			$("#lfq-drug-edit2").linkbutton(
					{
						onClick : function() {
							var rows = $("#lfq-tb-drugChuku").datagrid(
									"getSelections");
							if (rows.length == 1) {
								$("#lfq-win-editKuCun").window("open");
								//初始化表单
								var kcNo = rows[0].kcNo;
								$.getJSON("selectInventoryById", {
									"kcNo" : kcNo
								},
										function(data) {
											$("#lfq-form-editKuCun").form(
													"load", data);
										});

							} else {
								$.messager.alert("提示", "只能选择一条记录，您目前选择了"
										+ rows.length + "条!");
							}
						}
					});

			//编辑事件
			$("#lfq-btn-editKucunOk").linkbutton({
				onClick : function() {
					//用form提交
					$('#lfq-form-editKuCun').form('submit', {
						onSubmit : function(param) {

						},
						success : function(data) {
							var json = eval("(" + data + ")");
							$.messager.alert("提示", json.reason);
							$("#lfq-tb-drugChuku").datagrid("reload");
							$("#lfq-win-editKuCun").window("close");
						}
					});
				}
			});

			//打开出库窗口
			$("#lfq-drug-btn-ck").linkbutton({
				onClick : function() {
					$("#lfq-win-KuCun2").window("open");
					$("#lfq-form-chuku2").form("clear");
					var rows=$("#lfq-tb-chuku3").datagrid("getRows");
					for(var i=rows.length-1;i>=0;i--){
						var ind=$("#lfq-tb-chuku3").datagrid("getRowIndex",rows[i]);
						$("#lfq-tb-chuku3").datagrid("deleteRow",ind);
					}
				}
			});
			
			//药品onhang事件
			$("#lf1-chukuyaoping").combogrid({onChange:function(newValue, oldValue){
				$.getJSON("selectHisInventoryVolBykcNo", {
					"kcNo" : newValue
				},
				function(data) {
					$("#ypname").textbox("setValue",data.kcName);
					$("#ypprice").textbox("setValue",data.price);
					$("#kcnumber").textbox("setValue",data.kcNumber);
					$("#lfq-danwei").textbox("setValue",data.kcUnit)
					$("#lfq-chukushuliang").numberbox("setValue","");
				});
			}});
			
			//出库数量
			$("#lfq-chukushuliang").numberbox({onChange:function(newValue, oldValue){
				var a=$("#kcnumber").textbox("getValue");
				if(parseInt(a)<parseInt(newValue) || parseInt(newValue)<1){
					$("#lfq-chukushuliang").numberbox("setValue","");
				}
			}});
			
			//添加出库药品
			$("#lfq-btn-KucunTianjia").linkbutton({onClick:function(){
				var a=$("#lfq-chukushuliang").numberbox("getValue");
				var b=$("#kcnumber").textbox("getValue");
				if(parseInt(a)>0 && parseInt(b)>0){
					var c=$("#ypname").textbox("getValue");
					var d=$("#ypprice").textbox("getValue");
					var e=$("#lfq-danwei").textbox("getValue");
					
					var rows=$("#lfq-tb-chuku3").datagrid("getRows");
					if(rows.length>0){
						var k=0;
						for(var i=rows.length-1;i>=0;i--){
							if(rows[i].cpName==c){
								k++;
							}
						}
						if(k==0){
							$('#lfq-tb-chuku3').datagrid('appendRow', {
								'cpName' : [c],
								'cpPrice' : [d],
								'cpUnit' : [e],
								'ckNumber' : [b],
								'numbers' : [a]
							});
						}else{
							$.messager.alert("提示", "该药品已添加，您可以修改其数量！");
						}
					}else{
						$('#lfq-tb-chuku3').datagrid('appendRow', {
							'cpName' : [c],
							'cpPrice' : [d],
							'cpUnit' : [e],
							'ckNumber' : [b],
							'numbers' : [a]
						});
					}
				}
			}});
			
			//事件
			var chuku=0;
			$("#lfq-tb-chuku3").datagrid({onBeforeEdit:function(index, row){
				chuku=row.numbers;
			}});
			
			$("#lfq-tb-chuku3").datagrid({onAfterEdit:function(index, row, changes){
				if(parseInt(row.numbers)>parseInt(row.ckNumber)){
					$('#lfq-tb-chuku3').datagrid('updateRow',{
						index: index,
						row: {
							numbers: chuku
						}
					});
					$.messager.alert("提示", "出库数量不能大于库存数量！请重新修改！");
				}
			}});
			
			//出库
			$("#lfq-btn-KucunOk2").linkbutton({onClick:function(){
				var yfNo=$("#b1").combogrid("getValue");
				var ygNo=$("#b2").combogrid("getValue");
				var riqi=$("#b3").datebox("getValue");
				var rows=$("#lfq-tb-chuku3").datagrid("getRows");
				if(yfNo.length==0){
					$.messager.alert("提示", "出库药房不能为空！");
				}else if(ygNo.length==0){
					$.messager.alert("提示", "请选择负责人！");
				}else if(riqi.length==0){
					$.messager.alert("提示", "请选择日期！");
				}else if(rows.length==0){
					$.messager.alert("提示", "请先添加出库药品！");
				}else if(rows.length>0){
					var arrs=[];
					for(var i=0;i<rows.length;i++){
						arrs.push(rows[i].cpName);
						arrs.push(rows[i].cpPrice);
						arrs.push(rows[i].cpUnit);
						arrs.push(rows[i].ckNumber);
						arrs.push(rows[i].numbers);
					}
					$('#lfq-form-chuku2').form('submit', {
						url : 'submitChuku',
						onSubmit : function(param) {
							param.arr = arrs;
						},
						success : function(data) {
							var json = eval("(" + data + ")");
							$.messager.alert("提示", json.reason);
							//重新加载表格
							$("#lfq-tb-drugChuku").datagrid({
								url : 'selectHisInventoryAllDrug',
								method : 'post'
							});
							$("#lfq-win-KuCun2").window("close");
						}
					});
				}
			}});
			
			$('#lfq-tb-chuku3').datagrid({
				columns:[[
					{field:'cpName',width:'30%',title:'药品名称'},  
			        {field:'cpPrice',width:'15%',title:'价格'},
			        {field:'cpUnit',width:'15%',editor:{
						type:'combobox',
						options:{
							valueField:'label',
							textField:'value',
							panelHeight: 'auto',
							data: [{label:'箱',value:'箱'},{label:'盒',value:'盒'},{label:'斤',value:'斤'}
							    ,{label:'包',value:'包'},{label:'件',value:'件'},{label:'个',value:'个'}
							    ,{label:'卷',value:'卷'}]
						}
					},title:'药房单位'},  
			        {field:'ckNumber',width:'15%',title:'仓库数量'},
			        {field:'numbers',width:'15%',editor:{type:'numberbox',options:{precision:0,min:1}},title:'出库数量'},  
			        {field:'opt',title:'操作',width:50,align:'center',  
			            formatter:function(value,row,index){  
			                var btn = '<a class="easyui-linkbutton" onclick="deleteRowChuku(\''+row.cpName+'\')" href="javascript:void(0)">移除</a>';  
			                return btn;  
			            }
			        } 
				]]
			});


			
		});
	</script>
	
	<script type="text/javascript">
	    function deleteRowChuku(name){
	    	var rows=$("#lfq-tb-chuku3").datagrid("getRows");
	    	for(var i=0;i<rows.length;i++){
	    		if(rows[i].cpName==name){
	    			var index=$("#lfq-tb-chuku3").datagrid("getRowIndex",rows[i]);
	    			$("#lfq-tb-chuku3").datagrid("deleteRow",index);
	    			break;
	    		}
	    	}
	    }
	
	</script>

	<!-- 出库 -->
	<table id="lfq-tb-drugChuku" class="easyui-datagrid"
		data-options="url:'selectHisInventoryAllDrug',
			fitColumns:true,
			fit:true,
			singleSelect:true,
			pagination: true,
			pageSize: 5,
			pageList: [5, 10, 15, 20],
			rownumbers: true,
			toolbar: '#lfq-tool-chuku',
			border:false
			">
		<thead>
			<tr>
				<th data-options="field:'kcNo',width:'13%'">库存编号</th>
				<th data-options="field:'kcName',width:'20%'">药品名称</th>
				<th data-options="field:'kcTotalnumber',width:'13%'">历史数量</th>
				<th data-options="field:'kcNumber',width:'13%'">剩余数量</th>
				<th data-options="field:'kcUnit',width:'13%'">单位</th>
				<th data-options="field:'kcWarning',width:'13%'">预警值</th>
			</tr>
		</thead>
	</table>

	<div id="lfq-tool-chuku">
		<a id="lfq-drug-btn-ck" class="easyui-linkbutton"
			data-options="iconCls:'icon-chuku',plain:true">药品出库</a> <a
			id="lfq-drug-edit2" class="easyui-linkbutton"
			data-options="iconCls:'icon-edit',plain:true">编辑药品</a> <a
			id="lfq-drug-reload2" class="easyui-linkbutton"
			data-options="iconCls:'icon-reload',plain:true">刷新</a> <input
			id="lfq-drug-search2" class="easyui-searchbox"
			data-options="menu: '#lfq-menu-chuku', width: 260, prompt: '请输入查询信息'" />
	</div>

	<div id="lfq-menu-chuku" style="width: 120px">
		<div data-options="name:'药品名称'">药品名称</div>
		<div data-options="name:'库存编号'">库存编号</div>
	</div>

	<!-- 药品出库窗口 -->
	<div id="lfq-win-KuCun2" class="easyui-window" title="药品出库"
		data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true"
		style="width: 800px; height: 500px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<div class="easyui-layout" data-options="fit:true">
					<div data-options="region:'center'"
						style="padding: 8px; height: 38%;">
						<form id="lfq-form-chuku2" method="post">
							<table cellspacing="5px" cellpadding="0px" width="100%">
								<tr>
									<td><select name="yfNo" id="b1" class="easyui-combogrid"
										style="width: 200px;"
										data-options="
										    label: '出库药房',
										    panelWidth:90,
										    idField:'yfNo',
										    textField:'yfName',
										    panelHeight:'auto',
										    url:'selectHisPharmacyAll',
										    columns:[[
										        {field:'yfName',title:'药房名称',width:80}
										    ]]
									    "></select>&nbsp;&nbsp;&nbsp;&nbsp;
										<select name="ygNo" id="b2" class="easyui-combogrid"
										style="width: 200px;"
										data-options="
										    label: '负责人',
										    panelWidth:170,
										    idField:'ygNo',
										    panelHeight:'auto',
										    textField:'ygName',
										    url:'ckguanliyuanAll',
										    columns:[[
										        {field:'ygName',title:'姓名',width:80},
								                {field:'ygJob',title:'部门',width:80},
										    ]]
									    "></select>&nbsp;&nbsp;&nbsp;&nbsp;
										<input name="chuKuTime" id="b3" class="easyui-datebox"
										data-options="label: '出库时间', width: 180"></td>
								</tr>
								<tr>
									<td><select id="lf1-chukuyaoping" class="easyui-combogrid" style="width: 220px;"
										data-options="
										    label: '出库药品',
										    panelWidth:150,
										    idField:'kcNo',
										    panelHeight:'170',
										    textField:'kcName',
										    url:'selectHisInventorylLikeBykcNoAndCpNorms',
										    columns:[[
										        {field:'kcName',title:'药品名称',width:140}
										    ]]
									    "></select>&nbsp;&nbsp;&nbsp;&nbsp;

									</td>
								</tr>
								<tr>
									<td><input readonly="readonly"
										class="easyui-textbox" id="ypname" 
										data-options="label: '药品名称', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;
										<input readonly="readonly"
										class="easyui-textbox" id="ypprice" 
										data-options="label: '药品价格', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;
										<input readonly="readonly"
										class="easyui-textbox" id="kcnumber" 
										data-options="label: '药库数量', width: 200">
									</td>
								</tr>
								<tr>
									<td><input name="kcName" class="easyui-numberbox" id="lfq-chukushuliang" 
										data-options="label: '出库数量', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;
										出库单位&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="kcUnit"
										class="easyui-combobox" style="width: 120px" id="lfq-danwei" 
										data-options="panelHeight: 'auto'">
											<option value="箱">箱</option>
											<option value="盒">盒</option>
											<option value="斤">斤</option>
											<option value="包">包</option>
											<option value="件">件</option>
											<option value="个">个</option>
											<option value="箱">箱</option>
											<option value="卷">卷</option>
									</select>&nbsp;&nbsp;&nbsp;&nbsp; <a id="lfq-btn-KucunTianjia"
										class="easyui-linkbutton" data-options="width: 80">添加</a></td>
								</tr>

							</table>
						</form>
					</div>
					<div data-options="region:'south'"
						style="padding: 10px; height: 62%">
						<table id="lfq-tb-chuku3" class="easyui-datagrid" style="height: 226px"
							data-options="
								singleSelect: true,
								rownumbers: true,
								onClickCell: onClickCell,
								width: '100%'
							">
							<thead>
								<tr>
									<th data-options="field:'cpName',width:'30%'">药物名称</th>
									<th
										data-options="field:'cpPrice',width:'15%'">价格</th>
									<th
										data-options="field:'cpUnit',width:'15%',editor:{
																type:'combobox',
																options:{
																	valueField:'label',
																	textField:'value',
																	panelHeight: 'auto',
																	data: [{label:'箱',value:'箱'},{label:'盒',value:'盒'},{label:'斤',value:'斤'}
																	    ,{label:'包',value:'包'},{label:'件',value:'件'},{label:'个',value:'个'}
																	    ,{label:'卷',value:'卷'}]
																}
															}">药房单位</th>
									<th
										data-options="field:'ckNumber',width:'15%'">仓库数量</th>
									<th
										data-options="field:'numbers',width:'15%',editor:{type:'numberbox',options:{precision:0,min:1}}">出库数量</th>
								</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a id="lfq-btn-KucunOk2" class="easyui-linkbutton"
					data-options="iconCls:'icon-ok', plain:true">出库</a> <a
					class="easyui-linkbutton"
					data-options="iconCls:'icon-cancel', plain:true"
					href="javascript:$('#lfq-win-KuCun2').window('close');" onclick="">取消</a>
			</div>
		</div>
	</div>

	<!-- 编辑库存信息窗口 -->
	<div id="lfq-win-editKuCun" class="easyui-window" title="编辑库存信息"
		data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true"
		style="width: 400px; height: 300px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<form id="lfq-form-editKuCun" action="editKuCunAction" method="post">
					<div class="item">
						<input name="kcNo" readonly="readonly" class="easyui-textbox"
							data-options="label: '库存编号', width: 200">
					</div>
					<div class="item">
						<input name="kcName" class="easyui-textbox"
							data-options="label: '药品名称', width: 250, required:true">
					</div>
					<div class="item">
						<input name="kcTotalnumber" readonly="readonly"
							class="easyui-textbox" data-options="label: '历史数量', width: 250">
					</div>
					<div class="item">
						<input name="kcNumber" readonly="readonly" class="easyui-textbox"
							data-options="label: '剩余数量', width: 250">
					</div>
					<div class="item">
						单位 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select
							name="kcUnit" class="easyui-combobox" style="width: 120px"
							data-options="panelHeight: 'auto'">
							<option value="箱">箱</option>
							<option value="盒">盒</option>
							<option value="斤">斤</option>
							<option value="包">包</option>
							<option value="件">件</option>
							<option value="个">个</option>
							<option value="箱">箱</option>
							<option value="卷">卷</option>
						</select>
					</div>
					<div class="item">
						<input name="kcWarning" class="easyui-numberbox"
							data-options="label: '预警值', width: 250, required:true">
					</div>
				</form>
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a id="lfq-btn-editKucunOk" class="easyui-linkbutton"
					data-options="iconCls:'icon-ok', plain:true">确定</a> <a
					class="easyui-linkbutton"
					data-options="iconCls:'icon-cancel', plain:true"
					href="javascript:$('#lfq-win-editKuCun').window('close');"
					onclick="">取消</a>
			</div>
		</div>
	</div>
</body>
</html>